#include"Runtime/BattleFirePrefix.h"
#if _DEBUG
#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
#endif
#pragma comment(lib,"ws2_32.lib")
#ifdef BATTLEFIRE_OGL_RENDERER
#include"Runtime/CGI/OpenGL/OpenGLCGI.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#endif
#include "Runtime/Thread/ThreadPool.h"
#include "Runtime/Thread/RunnableTask.h"

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg){
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
	WNDCLASSEX wndClass;
	wndClass.cbClsExtra = 0;
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.cbWndExtra = 0;
	wndClass.hbrBackground = NULL;
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hIcon = NULL;
	wndClass.hIconSm = NULL;
	wndClass.hInstance = hInstance;
	wndClass.lpfnWndProc = WindowProc;
	wndClass.lpszClassName = L"BattleFireEngine";
	wndClass.lpszMenuName = NULL;
	wndClass.style = CS_VREDRAW | CS_HREDRAW;
	ATOM atom = RegisterClassEx(&wndClass);
	RECT rect;
	rect.left = 0;
	rect.right = 1280;
	rect.bottom = 720;
	rect.top = 0;
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
	HWND hwnd = CreateWindowEx(NULL, L"BattleFireEngine", L"BattleFireEngine Render Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);
	int width, height;
	GetClientRect(hwnd, &rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;
	BattleFire::CGI::sDynamicCGI=new BattleFire::OpenGLCGI();
	BattleFire::CGI::sDynamicCGI->Initialize(GetDC(hwnd),width,height);
	//BattleFire::ThreadPool::Get().Startup(12);
	//BattleFire::ThreadPool::Get().QueueTask(new BattleFire::XRunnableTask(0),BattleFire::AnyThread);
	MSG msg;
	while (true)
	{
		if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			glClearColor(0.1f,0.4f,0.6f,1.0f);
			glClear(GL_COLOR_BUFFER_BIT);
			SwapBuffers(GetDC(hwnd));
			Sleep(16);
		}
	}
	return 0;
}